﻿using System;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data;
using System.Web;

namespace Shuba.Web.master
{
    public partial class summary_period : basePage
    {
        bool IsView = false;
        string msg = "";
        private void BindData()
        {
            DateTime StartDate = Convert.ToDateTime(txtStartDate.Text);
            DateTime EndDate = Convert.ToDateTime(txtToDate.Text);

            string RegionalID, ClusterID, UnitID;

            if (ddlRegion.SelectedValue == "ALL") RegionalID = "%"; else RegionalID = ddlRegion.SelectedValue;
            if (ddlCluster.SelectedValue == "ALL") ClusterID = "%"; else ClusterID = ddlCluster.SelectedValue;
            if (ddlUnit.SelectedValue == "ALL") UnitID = "%"; else UnitID = ddlUnit.SelectedValue;

            Shuba.Business.Reports _Report = new Shuba.Business.Reports();
            DataSet ds = _Report.RPT_PERIOD_PERFORMANCE(RegionalID, ClusterID, UnitID, StartDate, EndDate);
            //DataView source = ds.Tables[0].DefaultView;
            gvList.DataSource = ds;
            gvList.DataBind();

            if (gvList.Rows.Count > 0) btnExcel.Enabled = true; else btnExcel.Enabled = false;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string acl = GetUserAccess("SUMMARY_PERIOD");

            IsView = IsUserView(acl);

            if (!IsView)
            {
                msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
            }

            if (!IsPostBack)
            {

                InitRegionList();
                InitClusterList(ddlRegion.SelectedValue);
                InitUnitList(ddlRegion.SelectedValue, ddlCluster.SelectedValue);

                txtStartDate.Text = ProcessingDate().ToString("dd MMMM yyyy");
                txtToDate.Text = ProcessingDate().ToString("dd MMMM yyyy");

                LabelTitle.Text = GetPageTitle("SUMMARY_PERIOD");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (Convert.ToDateTime(txtToDate.Text) >= Convert.ToDateTime(txtStartDate.Text))
            {
                BindData();
            }
            else
            {
                string msg = this.GetGlobalResourceObject("Messages", "InvalidDateRange").ToString();
                Alert(msg);
            }
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            if (Convert.ToDateTime(txtToDate.Text) >= Convert.ToDateTime(txtStartDate.Text))
            {
                string RegionCode = ddlRegion.SelectedValue;
                string RegionName = ddlRegion.SelectedItem.Text;
                string ClusterCode = ddlCluster.SelectedValue;
                string ClusterName = ddlCluster.SelectedItem.Text;
                string UnitCode = ddlUnit.SelectedValue;
                string UnitName = ddlUnit.SelectedItem.Text;
                string StartDate = txtStartDate.Text;
                string EndDate = txtToDate.Text;

                string sParam = "'" + RegionCode + "','" + RegionName + "','" + ClusterCode + "','" + ClusterName + "','" + UnitCode + "','" + UnitName + "','" + StartDate + "','" + EndDate + "'";

                System.Web.UI.ScriptManager.RegisterStartupScript(Page, this.GetType(), "Print", "PrintThis(" + sParam + ");", true);
            }
            else
            {
                string msg = this.GetGlobalResourceObject("Messages", "InvalidDateRange").ToString();
                Alert(msg);
            }
        }

        private void InitUnitList(string RegionID, string ClusterID)
        {
            var bl = new Shuba.Business.Branch();

            if (RegionID == "ALL") RegionID = "%";
            if (ClusterID == "ALL") ClusterID = "%";
            
            ddlUnit.DataTextField = "BranchName";
            ddlUnit.DataValueField = "BranchID";
            ddlUnit.DataSource = bl.GetListByRegionAndCluster(RegionID, ClusterID, HttpContext.Current.User.Identity.Name, ConfigurationManager.AppSettings["app.code"]);
            ddlUnit.DataBind();

            if (ddlUnit.Items.Count > 1)
            {
                ListItem ListAll = new ListItem();
                ListAll.Value = "ALL";
                ListAll.Text = " = ALL UNIT = ";
                ddlUnit.Items.Insert(0, ListAll);
            }
        }

        private void InitClusterList(string RegionalID)
        {
            var _Cluster = new Shuba.Business.Cluster();
            
            if (RegionalID == "ALL") RegionalID = "%"; 

            ddlCluster.DataTextField = "ClusterName";
            ddlCluster.DataValueField = "ClusterID";
            ddlCluster.DataSource = _Cluster.GetListByRegion(RegionalID, Session["UnitID"].ToString(), HttpContext.Current.User.Identity.Name, ConfigurationManager.AppSettings["app.code"]);
            ddlCluster.DataBind();

            if (ddlCluster.Items.Count > 1)
            {
                ListItem ListAll = new ListItem();
                ListAll.Value = "ALL";
                ListAll.Text = " = ALL CLUSTER = ";
                ddlCluster.Items.Insert(0, ListAll);
            }
        }

        private void InitRegionList()
        {
            var _Region = new Shuba.Business.Regional();

            ddlRegion.DataTextField = "RegionalName";
            ddlRegion.DataValueField = "RegionalID";
            ddlRegion.DataSource = _Region.GetList("%", Session["UnitID"].ToString(), HttpContext.Current.User.Identity.Name, ConfigurationManager.AppSettings["app.code"]);

            ddlRegion.DataBind();

            if (ddlRegion.Items.Count > 1)
            {
                ListItem ListAll = new ListItem();
                ListAll.Value = "ALL";
                ListAll.Text = " = ALL REGION = ";

                ddlRegion.Items.Insert(0, ListAll);
            }
        }

        protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitClusterList(ddlRegion.SelectedValue);
            InitUnitList(ddlRegion.SelectedValue, ddlCluster.SelectedValue);
        }

        protected void ddlCluster_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitUnitList(ddlRegion.SelectedValue, ddlCluster.SelectedValue);
        }

        protected void ddlUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlUnit.Items.Count == 0)
            {
                btnSearch.Enabled = false;
                btnExcel.Enabled = false;
            }
            else
            {
                btnSearch.Enabled = true;
                btnExcel.Enabled = true;
            }
        }
    }   
 }